package com.dgut.shopManage.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.shopManage.dao.ResultRepository;
import com.dgut.shopManage.entity.Results;
import com.dgut.shopManage.entity.dto.ResultsDTO;

@Service
@Transactional
public class ResultsService implements IResultsService {

	@Autowired
	private ResultRepository resultRepository;

	public void saveOrUpdate(Results results) {	
		resultRepository.save(results);
	}
	public void delete(Results results) {
		resultRepository.delete(results);
	}
	@Transactional(readOnly = true)
	public Results findOne(Long id) {
		return resultRepository.findOne(id);
	}
	@Transactional(readOnly = true)
	public List<Results> findAll() {
		return (List<Results>) resultRepository.findAll();
	}
	public void delete(Long id) {
		Results results = resultRepository.findOne(id);
		resultRepository.delete(results);
	}

	@Override
	public Page<ResultsDTO> findAll(Specification<Results> spec, Pageable pageable) {
		Page<Results> list = resultRepository.findAll(spec,pageable);
		List<ResultsDTO> dtoLists = new ArrayList<ResultsDTO>();
		for(Results entity : list.getContent()) {
			ResultsDTO dto = new ResultsDTO();
			ResultsDTO.entityToDto(entity, dto);
			dtoLists.add(dto);
		}
		return new PageImpl<ResultsDTO>(dtoLists,pageable,list.getTotalElements());
	}

}
